<?php
namespace app\admin\model;
use think\Db;
use think\Exception;
use think\Model;
use app\admin\model\Project;
class Column extends Model{

    //显示栏目
    public function show(){
        $info=$this->field(['seo_title','keywords','description'],true)->select();//查询本次请求数据
        $num=$this->count('id');//查询数据总条数
        $array=[
            'num'=>$num,
            'info'=>$info
        ];
        return $array;

    }

    //防止添加重复栏目名称(去重)
    public function Deduplication($columnname){
        $result=$this->where('name','=',$columnname)->find();
        if ($result){
            $fh=1;//查到内容
        }else{
            $fh=-1;//没内容
        }
        return $fh;

    }


    //防止修改栏目名称重复
    public function edit_Deduplication($columnname,$column_id){

        $id=$this->where('name','=',$columnname)->value("id");
        //如果是当前的栏目名称没做修改，那么不触发去重，用前端获取到的栏目id与，栏目名称查询出来的id对比
        if ($column_id==$id){
            $fh=1;
        }else if (empty($id)){
            $fh=1;
        }else{
            $fh=-1;
        }

        return $fh;

    }


    //防止修改目录名称重复
    public function dir_name_Deduplication($dir_name,$column_id){
        $id=$this->where('dir_name','=',$dir_name)->value("id");

        if ($column_id==$id){
            $fh=1;
        }else{
            $fh=-1;
        }

        return $fh;
    }




    //新增栏目
    public function addcolumn($columnname,$pid,$type,$directoryName,$cover_template,$list_templates,$article_templates,$seo_title,$keywords,$description){
        $result=$this->insert([
         'name'=>$columnname,
         'pid'=>$pid,
         'type'=>$type,
         'dir_name'=>$directoryName,
         'cover_template'=>empty($cover_template) ? "" :$cover_template,
         'list_templates'=>empty($list_templates) ? "" :$list_templates,
         'article_templates'=>empty($article_templates) ? "" :$article_templates,
         'seo_title'=>empty($seo_title) ? "" :$seo_title,
         'keywords'=>empty($keywords) ? "":$keywords,
         'description'=>empty($description) ? "":$description
        ]);

        return $result;

    }


    //修改栏目
    public function edit($id,$columnname,$directoryName,$cover_template,$list_templates,$article_templates,$seo_title,$keywords,$description){
        $result=$this->where("id=$id")->update([
           'name'=>$columnname,
            'dir_name'=>$directoryName,
            'cover_template'=>empty($cover_template) ? "" :$cover_template,
            'list_templates'=>empty($list_templates) ? "" :$list_templates,
            'article_templates'=>empty($article_templates) ? "" :$article_templates,
            'seo_title'=>empty($seo_title) ? "" :$seo_title,
            'keywords'=>empty($keywords) ? "":$keywords,
            'description'=>empty($description) ? "":$description
        ]);

        return $result;

    }




    /*
     通过栏目id查询栏目对应文件夹名称、对应模板名称
     */
    public function selectColumn($columnId){


     $result=$this->where("id","=",$columnId)->find();
     if ($result){
         $result=$result->toArray();
     }else{
         $result=[
           'dir_name'=>'',
             'article_templates'=>''
         ];
     }

     return $result;

    }



    //查询目录文件是否存在(数据库)
    public function find_dir_name($dir_name){
       $result= $this->where("dir_name","=",$dir_name)->find();
       if ($result){
           $result=$result->toArray();
       }else{
           $result=[];
       }

       return $result;

    }

    //查询栏目层级
    public function type($column_id){
        $result=$this->where("id","=",$column_id)->value('type');

        return $result;

    }


    //输出所有列表栏目
    public function list_colunm(){
        $result=$this->where("type",'=',2)->field(["id","name"])->select();
        return $result;

    }



    public function template($id,$type){
      switch ($type){
          case 1:
              $template=$this->where("id","=",$id)->value("cover_template");//封面模板
              break;
          case 2:
              $template=$this->where("id","=",$id)->value("list_templates");//列表模板
              break;
      }

      return $template;

    }


    //更具栏目id查询栏目信息
    public function sel_column($id){
        $result=$this->where("id","=",$id)->find();

        return $result;

    }






}
